<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 准备好一个区域 -->
    <div id="test"></div>

    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <script type="text/babel">
        // 1.创建组件
        class Life extends React.Component {
            //状态存储
            state = { opacity: 1 }

            //点击事件
            death = () => {
                //卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
            // 组件挂载完毕
            componentDidMount() {
                this.timer = setInterval(() => {
                    let { opacity } = this.state
                    opacity -= 0.1
                    if (opacity <= 0) opacity = 1
                    this.setState({ opacity })
                }, 200)
            }

            // 组件将要卸载
            componentWillUnmount() {
                //清除定时器
                clearInterval(this.timer)
            }

            //初始化渲染,状态更新之后  //相当于vue中的mounted
            render() {
                return (
                    <div>
                        <h2 style={{ opacity: this.state.opacity }}>React学不会怎么办</h2>
                        <button onClick={this.death}>不活了</button>
                    </div>
                )
            }
        }


        // 2. 渲染组件到页面
        ReactDOM.render(<Life />, document.getElementById('test'))


    </script>
</body>

</html>